﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FierceWar
{
	class MenuEntriesManager
	{
		private GameResources _resources;
		public List<MenuEntry> MenuEntries;
		private List<EventHandler> _screenEvents;
		public float OriginY = 30.0f;
		public float DistanceInterval = 50.0f;

		public MenuEntriesManager(GameResources resources,
			List<MenuEntry> menuEntries, List<EventHandler> screenEvents)
		{
			_resources = resources;
			MenuEntries = menuEntries;
			_screenEvents = screenEvents;
		}

		public MenuEntriesManager(GameResources resources,
			List<EventHandler> screenEvents)
		{
			_resources = resources;
			MenuEntries = new List<MenuEntry>();
			_screenEvents = screenEvents;
		}

		public void InitializePositions()
		{
			foreach (MenuEntry mEntry in MenuEntries)
			{
				mEntry.Position = new Vector2(
					(_resources.GraphicsDevice.Viewport.Width - mEntry.FontText.X) / 2,
					OriginY);
				OriginY += DistanceInterval;
			}
		}

		private void DisableAllOtherMenuEntries(MenuEntry menuEntry)
		{
			foreach (MenuEntry mEntry in MenuEntries)
			{
				if (mEntry != menuEntry)
				{
					mEntry.FunctionEnable = false;
				}
			}
		}

		private void EnableAllMenuEntries()
		{
			foreach (MenuEntry mEntry in MenuEntries)
			{
				mEntry.FunctionEnable = true;
			}
		}

		public void Update(GameTime gameTime)
		{
			for (int i = 0; i < MenuEntries.Count; i++)
			{
				MenuEntries[i].Update(gameTime);
				if (MenuEntries[i].IsBlink)
				{
					DisableAllOtherMenuEntries(MenuEntries[i]);
				}
				else if (MenuEntries[i].State == MenuEntry.MenuEntryState.Released)
				{
					EnableAllMenuEntries();
					_screenEvents[i].Invoke(this, new EventArgs());
				}
			}
		}

		public void Draw()
		{
			foreach (MenuEntry mEntry in MenuEntries)
			{
				mEntry.Draw();
			}
		}
	}
}
